// pages/consumptionRecord/consumptionRecord.js
const request = require("../../utils/request")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consumptions: [],
    queryParam: {
      currPage: 1,
      pageSize: 20,
    },
    total:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getRecordList()
  },
  getRecordList(id) {
    const token = wx.getStorageSync('token')
    if (!token) return wx.showToast({
      icon: "error",
      title: '请先登录',
    })
    const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
    if (!userinfo.mobile) return wx.showToast({
      icon: "error",
      title: '请求完善用户信息',
    })
    let that = this
    request({
      url: `/consumptionInformation/${userinfo.id}`,
      data:that.data.queryParam,
      header: {
        "X-Token": token
      },
      success: (res) => {
        console.log('获取成功', res);
        that.setData({
          consumptions: res.data.records,
          total:res.data.total
        })
      },
      fail: (err) => {
        console.log('获取失败', err);
      }
    })
  },
  lower() {
    console.log('触底数据', this.data.total);
    if (this.data.consumptions.length < this.data.total) {
      this.setData({
        "queryParam.currPage": this.data.queryParam.currPage + 1
      })

      this.getPushList()
    }
  },
  getPushList() {
    const that = this
    const token = wx.getStorageSync('token')
    request({
        url: '/consumptionInformation',
        data: that.data.queryParam,
        header: {
            "X-Token": token
        },
        success(data) {
            const list = that.data.consumptions
            data.data.records.forEach(item => {
                list.push(item)
            });
            console.log('push新数组', list);
            that.setData({
                consumptions: list
            })

        },
        fail(err) {
            console.log('请求失败：', err);
        }
    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})